package com.yinhai.message.implement.email.adapter;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.yinhai.message.api.util.MessageType;
import com.yinhai.message.api.vo.TMMessageVo;
import com.yinhai.message.core.adapterManager.IAdapter;
import com.yinhai.message.core.event.IMessageEvent;
import com.yinhai.message.core.exception.ResultsHandle;
import com.yinhai.message.implement.email.event.EmailMessageEvent;
import com.yinhai.message.implement.email.sender.EmailMessageSender;

@Component("emailAdapter")
public class EmailAdapter implements IAdapter, ApplicationListener<EmailMessageEvent>{

	private static final Logger LOGGER = LoggerFactory.getLogger(EmailAdapter.class);

	@Resource(name = "resultsHandle")
	private ResultsHandle resultsHandle;
	
	@Resource(name = "emailMessageSender")
	private EmailMessageSender emailMessageSender;
	
	@Override
	public String getSenderType() {
		return MessageType.EMAIL;
	}
	
	@Override
	public IMessageEvent getEven(TMMessageVo vo) {
		return new EmailMessageEvent(vo);
	}
	
	@Async
	@Override
	public void onApplicationEvent(EmailMessageEvent event) {
		LOGGER.debug("Email处理事件===============================================");
		TMMessageVo messageSendVO = event.getMessageVo();
		emailMessageSender.sendMessage(messageSendVO);
		String mgid = messageSendVO.getMessage().getMgid();
		resultsHandle.handleResults(messageSendVO.getUserlist(), mgid, getSenderType());
	}

}
